package com.yc.javaee.tomcat.impl;

import com.google.gson.Gson;
import com.yc.javaee.bean.Result;
import com.yc.javaee.tomcat.HttpServletRequest;
import com.yc.javaee.tomcat.HttpServletResponse;
import com.yc.javaee.tomcat.ServletException;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;

@WebServlet("/login.s")
public class LoginServlet extends HttpServletImpl{
    String[] users = {"yc","zs","ls"};
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Result result = null;
        for (String user : users) {
            if(user.equals(username) && "123".equals(password)){
                result = new Result(1, "登录成功", null);
                request.getSession().setAttribute("loginedUser",username);
                break;
            }
        }
        if(result == null){
            result = new Result(0, "用户名或密码错误", null);
        }
        response.setStatus(200,"OK");
        response.setContentType("application/json;charset=utf-8");
        String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }
}
